#ifndef BHXJWAVESIN_H
#define BHXJWAVESIN_H
#include<QOpenGLWidget>
#include<QPainter>
#include<QList>
#include<mutex>
#include<QtMath>
#include<QTimer>
#include "BHXJControls_global.h"
using namespace std;
class BHXJCONTROLS_EXPORT BHXJWaveSin : public QOpenGLWidget
{
    Q_OBJECT

    ///相位移速
    Q_PROPERTY(double Speed READ Speed WRITE setSpeed)
    ///采样
    Q_PROPERTY(uint Samples READ Samples WRITE setSamples)
    ///浪高
    Q_PROPERTY(double WaveHeight READ WaveHeight WRITE setWaveHeight)
    ///波浪密度
    Q_PROPERTY(double WaveDensity READ WaveDensity WRITE setWaveDensity)
    ///波浪相位差
    Q_PROPERTY(double PhaseDifference READ PhaseDifference WRITE setPhaseDifference)
    ///进度值
    Q_PROPERTY(double Value READ Value WRITE setValue)
    ///圆形
    Q_PROPERTY(bool BeEllipse READ BeEllipse WRITE setBeEllipse)

    Q_PROPERTY(QColor FrameColor READ FrameColor WRITE setFrameColor)

    Q_PROPERTY(QColor WaveColor READ WaveColor WRITE setWaveColor)


public:
    ///采样
    uint Samples(){return _Samples;}
    ///采样设置
    void setSamples(uint value){uint temp=value<1?1:value;  _Samples=temp;this->sinYFlag=true;this->Refrush();}
    double Speed(){return _Speed;}
    void setSpeed(double value){_Speed=value;this->Refrush();}
    double WaveHeight(){return _WaveHeight;}
    void setWaveHeight(double value){_WaveHeight=value;this->Refrush();}
    double WaveDensity(){return _WaveDensity;}
    void setWaveDensity(double value){double temp=value<0.001?0.001:value;_WaveDensity=temp;this->Refrush();}
    double PhaseDifference(){return _PhaseDifference;}
    void setPhaseDifference(double value){ double temp=value>1?1:(value<0?0:value); _PhaseDifference=temp;this->Refrush();}
    double Value(){return _Value;}
    void setValue(double value){double temp=value>1?1:(value<0?0:value);_Value=temp;}
    bool BeEllipse(){return _BeEllipse;}
    void setBeEllipse(bool value){_BeEllipse=value;this->RefrushMask();}
    QColor FrameColor(){return _FrameColor;}
    void setFrameColor(QColor value){_FrameColor=value; this->Refrush();}
    QColor WaveColor(){return _WaveColor;}
    void setWaveColor(QColor value){_WaveColor=value;}


private:
    uint _Samples=64;
    double _Speed=1.0;
    double _WaveHeight=0.25;
    double _WaveDensity=1;
    double _PhaseDifference=0.25;
    double _Value=0.5;
    bool _BeEllipse=true;
    QColor _FrameColor=QColor::fromRgb(240,240,240);
    QColor _WaveColor=Qt::blue;
public:
    explicit BHXJWaveSin(QWidget *parent = nullptr);
signals:
    void Refrush();
    void RefrushMask();

protected:
    void paintEvent(QPaintEvent *event) override;

    QTimer *timer;
    void timeoutfun();

    double offset=0;


    QList<double> sinY;
    mutex sinYlock;
    void updateSinY();
    bool sinYFlag=true;

    void updateMask();
};

#endif // BHXJWAVESIN_H
